package com.guogee.pdb.dao;

import com.guogee.pdb.bean.FamilyMember;
import com.guogee.pdb.db.DBHelper.FamilyMembersCollection;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;

public class FamilyMemberDAO extends ADao<FamilyMember> {

	public FamilyMemberDAO() {
		dbCollection = dbHelper.getIsmartDB().getCollection(FamilyMembersCollection.COLLECTION_NAME);
	}

	@Override
	public boolean insertItem(FamilyMember obj) {
		if(insertItem1(obj)!=null){
			return true;
		}
		return false;
	}

	@Override
	public FamilyMember parstItemFromCursor(DBCursor cursor) {
		return parstDBObject(cursor.next());
	
	}

	@Override
	public String insertItem1(FamilyMember obj) {
		// TODO Auto-generated method stub
		DBObject dbObj = new BasicDBObject();
		dbObj.put(FamilyMembersCollection.FAMILY_ID, obj.getFamilyId());
		dbObj.put(FamilyMembersCollection.MEMBER_ID, obj.getMemberId());
		dbObj.put(FamilyMembersCollection.ADDED_TIME, obj.getAddedTime());
		if(checkWritenResult(dbCollection.insert(dbObj))){
			return dbObj.get(FamilyMembersCollection.ID).toString();
		}
		return null;
	}

	@Override
	public FamilyMember parstDBObject(DBObject dbObj) {
		// TODO Auto-generated method stub
		FamilyMember object = new FamilyMember();
		object.setId(dbObj.get(FamilyMembersCollection.ID).toString());
		object.setFamilyId(dbObj.get(FamilyMembersCollection.FAMILY_ID).toString());
		object.setMemberId(dbObj.get(FamilyMembersCollection.MEMBER_ID).toString());
		object.setAddedTime(Long.valueOf(dbObj.get(FamilyMembersCollection.ADDED_TIME).toString()));
		return object;
	}
}
